package com.lukk.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lukk.constant.Const;
import com.lukk.dao.InternalWorkerDAO;
import com.lukk.pojo.InternalWorker;
import com.lukk.pojo.Province;
import com.lukk.service.IInternalWorkerService;
import com.lukk.util.AESUtils;
import com.lukk.util.MD5Utils;

/**
 * 
 * InternalWorkerServiceImpl 上午10:25:06
 * 
 * Copyright zhaocj Inc. All rights reserved.
 * Love ME Like Justin Bieber.
 */
@Service("internalWorkerService")
public class InternalWorkerServiceImpl implements IInternalWorkerService {
	@Autowired
	private InternalWorkerDAO internalWorkerDAO;

	public InternalWorkerDAO getInternalWorkerDAO() {
		return internalWorkerDAO;
	}

	public void setInternalWorkerDAO(InternalWorkerDAO internalWorkerDAO) {
		this.internalWorkerDAO = internalWorkerDAO;
	}

	@Override
	public boolean selectByPrimaryKey(InternalWorker internalWorker, HttpSession session) {
		// 如果请求验证对象的姓名或者密码为空
		if (StringUtils.isEmpty(internalWorker.getWorkerCode()) || StringUtils.isEmpty(internalWorker.getPassword())) {
			return false;
		}
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("id", internalWorker.getId());
		queryMap.put("workerCode", internalWorker.getWorkerCode());
		InternalWorker retInternalWorker = internalWorkerDAO.selectByPrimaryKey(queryMap);
		if (null == retInternalWorker) {
			return false;
		} else {
			if (internalWorker.getPassword().equals(retInternalWorker.getPassword())) {
				//将登陆成功的用户添加在session中
				session.setAttribute(Const.SESSION_USER, retInternalWorker);
				return true;
			} else {
				return false;
			}
		}
	}

	@Override
	public boolean modifyByPrimaryKey(InternalWorker internalWorker) {
		String saltCode = MD5Utils.getRandomString(8);
		String salt= "";
		try {
			 salt = AESUtils.aesEncrypt(saltCode, AESUtils.SALTKEY);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String password = MD5Utils.GetSaltCode(internalWorker.getPassword(), saltCode);
		internalWorker.setSalt(salt);
		internalWorker.setPassword(password);
		internalWorkerDAO.modifyByPrimaryKey(internalWorker);
		return true;
	}

	@Override
	public Map<String, Object> selectByPage(Integer offset, Integer limit, String sort, String order,
			InternalWorker internalWorker) {
		Map<String, Object> rets = new HashMap<String, Object>();
		internalWorker.getPageUtil().setOrderByCondition(sort);
		internalWorker.getPageUtil().setQueryCondition(order);
		List<InternalWorker> beans = internalWorkerDAO.selectByPage(internalWorker);
		int totalSize = beans.size();
		int toSize = (totalSize - offset) <= limit ? totalSize : offset + limit;
		rets.put("rows", beans.subList(offset, toSize));
		rets.put("total", totalSize);
		return rets;

	}

	@Override
	public InternalWorker selectInternalWorkerByProvince(Province province) {
		return internalWorkerDAO.selectInternalWorkerByProvince(province);
	}
}
